package com.unei.httpclient;

import org.apache.http.HttpClientConnection;
import org.apache.http.conn.HttpClientConnectionManager;

import java.util.concurrent.TimeUnit;

/**
 * Created by sunlei on 2015/4/16.
 */
public class IdleConnectionManager extends Thread{
    private final HttpClientConnectionManager httpClientConnectionManager;

    private volatile boolean shutdown;

    public IdleConnectionManager(HttpClientConnectionManager httpClientConnectionManager){
        this.httpClientConnectionManager=httpClientConnectionManager;
    }

    @Override
    public void run() {
        try {
            while (!shutdown) {
                synchronized (this) {
                    wait(1000);
                    httpClientConnectionManager.closeExpiredConnections();;
                    httpClientConnectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public void shutdown(){
        shutdown=true;
        synchronized (this) {
            notifyAll();
        }
    }
}
